package com.ebupt.migu.music.common.utils;

import cn.hutool.json.JSONObject;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 常用工具方法
 *
 * @author liuyangyang@cd.ebupt.com
 */
public class CommonUtil {

    /**
     * 返回abc三者最大值
     *
     * @param a
     * @param b
     * @param c
     * @return
     */
    public static int maxValue(int a, int b, int c) {
        int temp = a > b ? a : b;
        return temp > c ? temp : c;
    }

    public static void main(String[] args) {
        System.out.println(maxValue(100, 100, 101));
    }

    /**
     * 获取指定属性值
     *
     * @param obj
     * @param element
     * @return
     */
    public static String getElement(Object obj, String element) {
        return String.valueOf(((JSONObject) obj).getByPath(element));
    }

    /**
     * 判断反查结果是否在标签条件中
     *
     * @param a
     * @param b
     * @return
     */
    public static boolean isContains(List<String> a, String b) {
        boolean contains = true;
        if (null != a && a.size() != 0) {
            if (!"".equals(b) && null != b) {
                if (!a.contains(b)) {
                    contains = false;
                }
            } else {
                contains = false;
            }
        }
        return contains;
    }

    /**
     * 拼接查询参数
     * @param list
     * @return
     */
    public static String getParams(List<String> list) {
        String params = "";

        if (list != null && list.size() > 0) {
            for (String element : list) {
                params += "%22" + element + "%22 ";
            }
        }

        if (!"".equals(params)) {
            params = params.substring(0, params.length() - 1);
        } else {
            params = "%22%22";
        }

        return params;
    }

    /**
     * 解析静态标签
     *
     * @param attribute
     * @return
     */
    public static String analysis(String attribute) {
        Gson gson = new Gson();
        Map<String, Object> groupAttributeMap = gson.fromJson(attribute, Map.class);

        // 组装查询条件
        List<String> comTypeList = new ArrayList<>();
        List<String> ageList = new ArrayList<>();
        List<String> areaNameList = new ArrayList<>();
        List<String> userLevelList = new ArrayList<>();
        List<String> chnIdList = new ArrayList<>();
        List<String> phoneSystemList = new ArrayList<>();
        String sex = "";
        String loginStatus = "";
        if (groupAttributeMap.get("comType") != null && !"".equals(String.valueOf(groupAttributeMap.get("comType")))) {
            comTypeList = (List) groupAttributeMap.get("comType");
        }
        if (groupAttributeMap.get("age") != null && !"".equals(String.valueOf(groupAttributeMap.get("age")))) {
            ageList = (List) groupAttributeMap.get("age");
        }
        if (groupAttributeMap.get("areaName") != null && !"".equals(String.valueOf(groupAttributeMap.get("areaName")))) {
            areaNameList = (List) groupAttributeMap.get("areaName");
        }
        if (groupAttributeMap.get("userLevel") != null && !"".equals(String.valueOf(groupAttributeMap.get("userLevel")))) {
            userLevelList = (List) groupAttributeMap.get("userLevel");
        }
        if (groupAttributeMap.get("chnId") != null && !"".equals(String.valueOf(groupAttributeMap.get("chnId")))) {
            chnIdList = (List) groupAttributeMap.get("chnId");
        }
        if (groupAttributeMap.get("phoneSystem") != null && !"".equals(String.valueOf(groupAttributeMap.get("phoneSystem")))) {
            phoneSystemList = (List) groupAttributeMap.get("phoneSystem");
            for (int i = 0; i < phoneSystemList.size(); i++) {
                if (phoneSystemList.get(i).equals("1")) {
                    phoneSystemList.set(i, "android");
                } else if (phoneSystemList.get(i).equals("1")) {
                    phoneSystemList.set(i, "ios");
                }
            }
        }
        if (groupAttributeMap.get("sex") != null && !"".equals(String.valueOf(groupAttributeMap.get("sex")))) {
            sex = groupAttributeMap.get("sex").toString();
        }
        if (groupAttributeMap.get("loginStatus") != null && !"".equals(groupAttributeMap.get("loginStatus").toString())) {
            loginStatus = groupAttributeMap.get("loginStatus").toString();
        }
        // 拼接查询条件
        String params = "";
        if (!((sex == null || "".equals(sex))
                && (loginStatus == null || "".equals(loginStatus))
                && (comTypeList == null || comTypeList.size() == 0)
                && (ageList == null || ageList.size() == 0)
                && (areaNameList == null || areaNameList.size() == 0)
                && (userLevelList == null || userLevelList.size() == 0)
                && (chnIdList == null || chnIdList.size() == 0)
                && (phoneSystemList == null || phoneSystemList.size() == 0))) {
            sex = String.format("sex:(%s)", "%22" + groupAttributeMap.get("sex") + "%22"); // "双引号转义 %22
            loginStatus = String.format("login_status:(%s)", "%22" + groupAttributeMap.get("loginStatus") + "%22"); // "双引号转义 %22
            String comType = String.format("com_type:(%s)", CommonUtil.getParams(comTypeList));
            String age = String.format("age:(%s)", CommonUtil.getParams(ageList));
            String areaName = String.format("area_name:(%s)", CommonUtil.getParams(areaNameList));
            String userLevel = String.format("user_level:(%s)", CommonUtil.getParams(userLevelList));
            String phoneSystem = String.format("phone_system:(%s)", CommonUtil.getParams(phoneSystemList));
            String chnId = String.format("chn_id:(%s)", CommonUtil.getParams(chnIdList));
            params = "(" + comType + "AND+" + sex + "AND+" + age + "AND+" + areaName + "AND+" + userLevel + "AND+" + loginStatus + "AND+" + chnId + "AND+" + phoneSystem + ")";
        }
        return params;
    }
}
